package com.nut.spp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nut.spp.api.req.BannerQueryReq;
import com.nut.spp.common.Result;
import com.nut.spp.entity.Banner;
import com.nut.spp.mapper.BannerMapper;
import com.nut.spp.service.BannerService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Slf4j
@Service
public class BannerServiceImpl implements BannerService {

    @Autowired
    private BannerMapper bannerMapper;

    @Override
    public Result<List<Banner>> list() {
        LambdaQueryWrapper<Banner> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Banner::getStatus, 1);
        queryWrapper.orderByAsc(Banner::getSort);
        List<Banner> list = bannerMapper.selectList(queryWrapper);
        return Result.ok(list);
    }

    @Override
    public Result<IPage<Banner>> listPage(BannerQueryReq req) {
        IPage<Banner> list = bannerMapper.listPage(req.newPage(), req.getTitle(),
                req.getStatus(), req.getType());
        return Result.ok(list);
    }

    @Override
    public Result<Banner> getById(Integer id) {
        Banner banner = bannerMapper.selectById(id);
        if (banner == null) {
            return Result.fail("未找到导航图片信息");
        }
        return Result.ok(banner);
    }

    @Override
    public Result add(Banner banner) {
        bannerMapper.insert(banner);
        return Result.ok();
    }

    @Override
    public Result updateById(Banner banner) {
        bannerMapper.updateById(banner);
        return Result.ok();
    }

    @Override
    public Result deleteByIds(Set<Integer> ids) {
        bannerMapper.deleteBatchIds(ids);
        return Result.ok();
    }
}
